/**
 * LoginController.java
 */
package org.yate.oa.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yate.oa.model.LoginEntity;
import org.yate.oa.service.imp.LoginService;
import org.yate.oa.utils.Constants;
import org.yate.oa.utils.MessageDigestUtils;

/**
 * @author Yate
 * @date 2013年9月18日
 * @version 1.0
 */
@Controller
@RequestMapping("/static")
public class LoginController extends BaseController {
	
	private LoginService loginsvr;
	
//	@Override
//	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//		res.setCharacterEncoding("utf-8");
//		req.setCharacterEncoding("utf-8");
//		res.setContentType("text/html; charset=utf-8");
//		String type = req.getParameter("type");
//		if ("login".equals(type)) {
//			login(req, res);
//		} else {
//			logout(req, res);
//		}
//	}

	protected void login(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		try {
			String email = req.getParameter("email");
			String password = req.getParameter("password");
			LoginEntity loginEntity = new LoginEntity();
			loginEntity.setEmail(email);
			loginEntity.setPassword(MessageDigestUtils.toMD5Hex(password));
			loginEntity = loginsvr.getEntitieBy(loginEntity);
			/*if (loginEntity != null) {
				IPrivilegeService roleService = this.springCtx.getBean(IPrivilegeService.class);
				List<PrivilegeEntity> roleEntity = roleService.getTreeByUID(loginEntity.getUserId());
				req.getSession().setAttribute("role", roleEntity);
				req.getSession().setAttribute(Constants.SESSION_USER_ENTITY, loginEntity);
				Integer zlid = loginsvr.getZLId(loginEntity.getUserId());
				int zlidvalue = zlid == null ? 0 : zlid.intValue();
				req.getSession().setAttribute("zlid", zlidvalue);
				res.sendRedirect(req.getContextPath() + "/index.jsp");
				IRelationService rsvr = this.springCtx.getBean(IRelationService.class);
				RelationEntity re = rsvr.getEntityBy(new RelationEntity(loginEntity.getUserId(), 0));
				req.getSession().setAttribute("orgid", re.getOrgId());
				req.getSession().setAttribute("iszl", loginEntity.getUserId().intValue() == zlidvalue);
			} else {
				req.setAttribute("text", "用户名或密码错误");
				req.getRequestDispatcher("/login.jsp").forward(req, res);
			}*/
			res.sendRedirect(req.getContextPath() + "/index.jsp");
		} catch (Exception e) {
			super.log.error(e.getMessage(), e);
			req.setAttribute("text", "登录异常");
			req.getRequestDispatcher("/login.jsp").forward(req, res);
		}

	}

	protected void logout(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
		req.getSession().removeAttribute(Constants.SESSION_USER_ENTITY);
		req.getSession().removeAttribute("rols");
		req.getSession().invalidate();
		res.sendRedirect(req.getContextPath() + "/login.jsp");
	}
}
